package com.ky.manage.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.jpeng.jptabbar.JPTabBar;
import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.activity.LoginActivity;
import com.ky.manage.activity.MainActivity;
import com.ky.manage.activity.OrderListActivity;
import com.ky.manage.activity.OutletsActivity;
import com.ky.manage.activity.PersonalInfoActivity;
import com.ky.manage.activity.PwdLoginActivity;
import com.ky.manage.activity.SettingActivity;
import com.ky.manage.activity.emergency.EmergencyListActivity;
import com.ky.manage.activity.indoor.IndoorMendListActivity;
import com.ky.manage.activity.indoor.IndoorSecurityCheckActivity;
import com.ky.manage.activity.outdoor.OutdoorMendListActivity;
import com.ky.manage.activity.outdoor.OutdoorSecurityCheckActivity;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.base.BaseFragment;
import com.ky.manage.constant.OverallData;
import com.ky.manage.model.LoginUserInfo;
import com.ky.manage.utils.StringUtil;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.log.ObjectUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import static com.ky.manage.constant.OverallData.Emergency_Pei_Xun;
import static com.ky.manage.constant.OverallData.Emergency_Shi_Gu_Deal;
import static com.ky.manage.constant.OverallData.Emergency_Xuan_Chuan;
import static com.ky.manage.constant.OverallData.Emergency_Yan_Lian;

/**
 * 我的页面
 */
public class MineFragment extends BaseFragment {
    private JPTabBar mTabBar;
    private View loginUserInfoView, toLoginView;
    private TextView mUserNameTv, mUserTypeTv;
    private ImageView mUserHeadIv;

    @Override
    protected int layoutId() {
        return R.layout.layout_mine_fragment;
    }

    @Override
    protected void initView(View rootView) {
        mTabBar = ((MainActivity)getActivity()).getTabbar();
        //消息数字标识
//        mTabBar.showBadge(0,"文字",true);
//        mTabBar.showCircleBadge(0,true);
//        mTabBar.hideBadge(0);
        LoginUserInfo loginUserInfo = BaseApplication.getLoginUserInfo();
        findView(R.id.login_user_info_ll, v -> {
            if (BaseApplication.isValidLogin()) {
                toActivity(new Intent(mContext, PersonalInfoActivity.class));
            }
        });
        mUserHeadIv = findView(R.id.user_header_iv);
        ZyUtils.getInstance().insertLog("loginUserInfo", ObjectUtil.objectToString(loginUserInfo));
        if (null != loginUserInfo) {
            //ImageLoaderUtils.loadImage(mContext, loginUserInfo.userHeadUrl, mUserHeadIv);
            loadHeader(loginUserInfo.userHeadUrl);
        } else {
            mUserHeadIv.setImageDrawable(mContext.getDrawable(R.mipmap.default_user_head));
        }
        loginUserInfoView = findView(R.id.login_user_info);
        mUserNameTv = findTextView(R.id.nick_name_tv, null != loginUserInfo ? loginUserInfo.nickName : "");
        mUserTypeTv = findTextView(R.id.user_type_tv, null != loginUserInfo ? loginUserInfo.userTypeDesc : "");
        mUserTypeTv.setVisibility(TextUtils.isEmpty(mUserTypeTv.getText().toString()) ? View.GONE : View.VISIBLE);
        toLoginView = findView(R.id.to_login_tv, v -> {
            //去登录
            toActivity(new Intent(getActivity(), OverallData.sEnableSmsCodeLogin ?
                    LoginActivity.class : PwdLoginActivity.class));
        });
        boolean isLogin = BaseApplication.isValidLogin();
        loginUserInfoView.setVisibility(isLogin ? View.VISIBLE : View.GONE);
        toLoginView.setVisibility(isLogin ? View.GONE : View.VISIBLE);
        findView(R.id.scan_code_iv, v -> {
            //扫码
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        findView(R.id.setting_iv, v -> {
            //设置
            toActivity(new Intent(mContext, SettingActivity.class));
        });
        findView(R.id.my_all_order, v -> {
            //订单
            startActivity(new Intent(mContext, OrderListActivity.class));
        });
        findView(R.id.to_pay_tv, v -> {
            //待付款
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        findView(R.id.to_receive_tv, v -> {
            //待收货
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        findView(R.id.to_comment_tv, v -> {
            //待评论
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        findView(R.id.pay_after_tv, v -> {
            //售后
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        findView(R.id.hu_nei_an_jian_tv, v -> {
            //户内安检
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                toActivity(new Intent(mContext, IndoorSecurityCheckActivity.class));
            }
        });
        findView(R.id.modify_log_tv, v -> {
            //户内整改记录
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                toActivity(new Intent(mContext, IndoorMendListActivity.class));
            }
        });
        findView(R.id.hu_wai_an_jian_tv, v -> {
            //户外巡检
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                toActivity(new Intent(mContext, OutdoorSecurityCheckActivity.class));
            }
        });
        findView(R.id.wu_wai_zheng_gai_tv, v -> {
            //户外整改
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                toActivity(new Intent(mContext, OutdoorMendListActivity.class));
            }
        });
        findView(R.id.ying_ji_yan_lian_tv, v -> {
            //应急演练
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                EmergencyListActivity.toEmergencyListActivity((BaseActivity) getActivity(),
                        Emergency_Yan_Lian);
            }
        });
        findView(R.id.ying_ji_pei_xun_tv, v -> {
            //应急培训
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                EmergencyListActivity.toEmergencyListActivity((BaseActivity) getActivity(),
                        Emergency_Pei_Xun);
            }
        });
        findView(R.id.xuan_chuan_log_tv, v -> {
            //宣传记录
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                EmergencyListActivity.toEmergencyListActivity((BaseActivity) getActivity(),
                        Emergency_Xuan_Chuan);
            }
        });
        findView(R.id.shi_gu_deal_log_tv, v -> {
            //事故处理记录
            if (BaseApplication.isValidLogin((BaseActivity)getActivity(), true)) {
                EmergencyListActivity.toEmergencyListActivity((BaseActivity) getActivity(),
                        Emergency_Shi_Gu_Deal);
            }
        });
        findView(R.id.tv_net_point, v ->{
            toActivity(new Intent(mContext, OutletsActivity.class));
        });
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginStateChange(LoginUserInfo event) {
        if (null == event) {
            return;
        }
        mUserNameTv.setText(event.nickName);
        mUserTypeTv.setText(event.userTypeDesc);
        mUserTypeTv.setVisibility(TextUtils.isEmpty(mUserTypeTv.getText().toString()) ? View.GONE : View.VISIBLE);
        loginUserInfoView.setVisibility(event.isLogin ? View.VISIBLE : View.GONE);
        toLoginView.setVisibility(event.isLogin ? View.GONE : View.VISIBLE);
        if (event.isLogin) {
            //ImageLoaderUtils.loadImage(mContext, event.userHeadUrl, mUserHeadIv);
            loadHeader(event.userHeadUrl);
        } else {
            mUserHeadIv.setImageDrawable(mContext.getDrawable(R.mipmap.default_user_head));
        }
    }

    private void loadHeader(String url) {
        Glide.with(this).asBitmap().centerCrop()
                .load(url)
                .placeholder(R.mipmap.default_user_head)
                .error(R.mipmap.default_user_head)
                .into(new BitmapImageViewTarget(mUserHeadIv){
                    @Override
                    protected void setResource(Bitmap resource) {
                        super.setResource(resource);
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        mUserHeadIv.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }
}
